﻿using MultiDAQ_Analysis.License;
using QLicense;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace MultiDAQ_Analysis.Util
{
    class LicenseGeneratorTool
    {
        //static void Main(string[] args)
        //{
        //    Console.WriteLine("许可证生成工具");
        //    Console.WriteLine("================");

        //    try
        //    {
        //        // 输入参数
        //        Console.Write("请输入客户的设备UID: ");
        //        string deviceUID = Console.ReadLine();

        //        Console.Write("请输入有效期（天数）: ");
        //        int days = int.Parse(Console.ReadLine());

        //        DateTime expireDate = DateTime.UtcNow.AddDays(days);

        //        // 加载私钥证书
        //        byte[] privateKeyData = File.ReadAllBytes("LicenseSign.pfx");

        //        Console.Write("请输入证书密码: ");
        //        SecureString password = GetSecureString();

        //        // 创建许可证
        //        var license = new MyTimeLicense("YourAppName", expireDate, deviceUID);
        //        string licenseString = LicenseHandler.GenerateLicenseBASE64String(
        //            license, privateKeyData, password);

        //        Console.WriteLine("\n生成的许可证:");
        //        Console.WriteLine("================");
        //        Console.WriteLine(licenseString);

        //        // 保存到文件
        //        string fileName = $"License_{DateTime.Now:yyyyMMdd_HHmmss}.lic";
        //        File.WriteAllText(fileName, licenseString);
        //        Console.WriteLine($"\n许可证已保存到: {fileName}");
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine($"错误: {ex.Message}");
        //    }

        //    Console.WriteLine("\n按任意键退出...");
        //    Console.ReadKey();
        //}

        private static SecureString GetSecureString()
        {
            var secureString = new SecureString();
            ConsoleKeyInfo keyInfo;

            do
            {
                keyInfo = Console.ReadKey(true);
                if (keyInfo.Key != ConsoleKey.Backspace && keyInfo.Key != ConsoleKey.Enter)
                {
                    secureString.AppendChar(keyInfo.KeyChar);
                    Console.Write("*");
                }
                else if (keyInfo.Key == ConsoleKey.Backspace && secureString.Length > 0)
                {
                    secureString.RemoveAt(secureString.Length - 1);
                    Console.Write("\b \b");
                }
            } while (keyInfo.Key != ConsoleKey.Enter);

            Console.WriteLine();
            return secureString;
        }
    }
}
